//
//  HKBaseModuleTabBarController.m
//  HKUtilities
//
//  Created by 胡锦涛 on 2018/10/30.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "HKLibBaseTabBarController.h"
#import "HKLibBaseNavController.h"
#import "HKLibBaseHeader.h"
#import "CommonBundle.h"
#import "BifrostHeader.h"
#import "UIKitModuleService.h"
@interface HKLibBaseTabBarController ()<UITabBarControllerDelegate>

@end

@implementation HKLibBaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
+ (UIViewController *)hk_generateViewControllerWithConfig:(NSMutableDictionary *)config {
    HKLibBaseTabBarController *tabBarViewController = [[HKLibBaseTabBarController alloc] init];
    
    // 控制器个数
    NSMutableArray * vcs = @[].mutableCopy;
    // 正常未选中状态图片
    NSMutableArray * normalImages = @[].mutableCopy;
    // 选中状态图片
    NSMutableArray * selectedImages = @[].mutableCopy;
    // 标题
    NSMutableArray * titles = @[].mutableCopy;
    
    
    if (config == nil || config.count <= 0) {
        
        vcs = @[@"HKUIKitViewController",@"HKComponentsViewController"].mutableCopy;
        
        normalImages = @[@"icon_tabbar_uikit",@"icon_tabbar_component"].mutableCopy;
        
        selectedImages =  @[@"icon_tabbar_uikit_selected",@"icon_tabbar_component_selected"].mutableCopy;
        
        titles = @[@"QMUIKit",@"Components"].mutableCopy;
    }else {
        vcs = config[@"vcs"];
        normalImages = config[@"normalImages"];
        selectedImages = config[@"selectedImages"];
        titles = config[@"titles"];
    }
    NSMutableArray * navs = @[].mutableCopy;
    for (int i=0;i<titles.count;i++) {
        //vc =  [Bifrost handleURL:kRouteUIKitPage];
        UIViewController *vc = [[NSClassFromString(vcs[i]) alloc]init];
        HKLibBaseNavController *nav = [[HKLibBaseNavController alloc]initWithRootViewController:vc];
        vc.tabBarItem = [HKUIHelper tabBarItemWithTitle:titles[i] image:[CommonBundle imageNamed:normalImages[i]] selectedImage:[CommonBundle imageNamed:selectedImages[i]] tag:i];
//        vc.tabBarItem = [[UITabBarItem alloc]initWithTitle:titles[i] image:[[UIImage imageNamed:normalImages[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImages[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//        [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:UIControlStateNormal];
//        [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:UIControlStateSelected];
        vc.title = titles[i];
        vc.hidesBottomBarWhenPushed = NO;
//        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -2);
        //vc.tabBarItem.badgeColor = [UIColor redColor];//小圆点标识
        vc.navigationController.interactivePopGestureRecognizer.delegate=(id)vc;
        [navs addObject:nav];
    }
    tabBarViewController.viewControllers = navs;
//    tabBarViewController.selectedIndex = 0;
//    tabBarViewController.selectedViewController = [vcs firstObject];
    return tabBarViewController;
}

- (void)hk_hideBottomLine {
    //去掉边框线条
    CGRect rect = self.tabBar.bounds;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];

    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 47)];
    backView.image = [UIImage imageNamed: @"tabbarBgImage"];
    [self.tabBar insertSubview:backView atIndex:0];
    self.tabBar.opaque = YES;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0) {
    return YES;
}

#pragma mark---------UITabBarControllerDelegate------
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    UIViewController * vc;
    vc = [(HKLibBaseNavController *)viewController topViewController];
}

- (UIImage *)noRenderingImageName:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    /**
     UIImageRenderingModeAutomatic  自动根据当前环境渲染
     UIImageRenderingModeAlwaysOriginal  使用原有的图片状态，不被Tint Color所渲染
     UIImageRenderingModeAlwaysTemplate  渲染图片
     */
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

@end
